package chapter03.d06;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

public class ThreadFactoryTest {

	public static void main(String[] args) {
		ExecutorService exec = Executors.newFixedThreadPool(3, new DaemonThreadFactory());
		for (int i = 0; i < 3; i++) {
			exec.submit(new Task(i));
		}
		exec.shutdown();
	}
}

class Task implements Runnable {
	int taskId;

	public Task(int taskId) {
		this.taskId = taskId;
	}

	@Override
	public void run() {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + "--taskId: " + taskId);

	}
}

class DaemonThreadFactory implements ThreadFactory {
	@Override
	public Thread newThread(Runnable r) {
		Thread t = new Thread(r);
		//t.setDaemon(true);
		return t;
	}

}
